package assetservicelogic

import (
	"context"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/BeanUtils"
	"property_service/utils/result"

	"github.com/zeromicro/go-zero/core/logx"
)

type AssetDepreciationEditLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAssetDepreciationEditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AssetDepreciationEditLogic {
	return &AssetDepreciationEditLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// AssetDepreciationEdit 更新折旧管理表项
func (l *AssetDepreciationEditLogic) AssetDepreciationEdit(in *property_service.AssetDepreciation) (*property_service.Response, error) {
	// 获取折旧管理表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).AssetDepreciation
	// 构建查询条件：根据ID查询
	do := m.WithContext(l.ctx).Where(m.ID.Eq(in.ID))
	// 执行更新操作，仅更新指定字段，不包含ID、TenantID、CreatedAt、UpdatedAt
	if _, err := do.UpdateColumns(&model.AssetDepreciation{
		AssetID:                  in.AssetID,                               // 资产ID
		AssetCode:                in.AssetCode,                             // 资产编码
		CurrentMonthDepreciation: in.CurrentMonthDepreciation,              // 本月折旧
		CardNumber:               in.CardNumber,                            // 卡片编号
		AssetName:                in.AssetName,                             // 资产名称
		AssetCategory:            in.AssetCategory,                         // 资产分类
		AssetCategoryID:          in.AssetCategoryID,                       // 资产分类ID
		DepreciationPeriod:       in.DepreciationPeriod,                    // 折旧期间
		DataIdentifier:           in.DataIdentifier,                        // 数据标识
		Warehouse:                in.Warehouse,                             // 库房
		UsingDept:                in.UsingDept,                             // 使用科室
		UsingDeptID:              in.UsingDeptID,                           // 使用科室ID
		OriginalValue:            in.OriginalValue,                         // 资产原值
		AccumulatedDepreciation:  in.AccumulatedDepreciation,               // 累计折旧额
		Depreciation:             in.Depreciation,                          //月折旧额
		DepreciationRate:         in.DepreciationRate,                      //月折旧率
		DepreciationRateYear:     in.DepreciationRateYear,                  //年折旧率
		DepreciationMethodID:     in.DepreciationMethodID,                  // 折旧方法ID
		DepreciationMethod:       in.DepreciationMethod,                    // 折旧方法
		PurchaseDate:             BeanUtils.GetDateFormat(in.PurchaseDate), // 购置日期
		UsefulLife:               in.UsefulLife,                            // 使用年限
		ExpectedUsefulLife:       in.ExpectedUsefulLife,                    // 预计使用年限
		DepreciatedMonths:        in.DepreciatedMonths,                     // 已折旧月数
		ResidualValue:            in.ResidualValue,                         // 残值
		Residual:                 in.Residual,                              //残值率
		StatusID:                 in.StatusID,                              // 状态ID
		StatusName:               in.StatusName,                            // 状态名称
		Remarks:                  in.Remarks,                               // 备注
		DepreciationStatus:       in.DepreciationStatus,                    // 折旧状态
		StockID:                  in.StockID,                               // 仓库ID
		UpdatedAtuser:            in.UpdatedAtuser,                         // 更新人
	}); err != nil {
		// 如果更新失败，返回错误响应
		return result.Err(err).Ed()
	}
	// 更新成功，返回成功响应
	return result.Suc().EditEd()
}
